//
//  LottieLoadingView.swift
//  rdb-led-banner
//
//  Created by radiobox3 on 2025/4/18.
//

import UIKit
import Lottie
import SnapKit

class LottieLoadingView: UIView {
    
    var timer: DispatchSourceTimer?
    var completion: (() -> Void)?

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.addSubview(launchLoadingImageView)
        self.launchLoadingImageView.snp.makeConstraints{
            $0.bottom.centerX.equalToSuperview()
            $0.size.equalTo(CGSize(width: 1125 / 3.0, height: 276 / 3.0))
        }
        self.addSubview(loadingLabel)
        self.loadingLabel.snp.makeConstraints{
            $0.bottom.equalToSuperview().offset(-32)
            $0.centerX.equalToSuperview()
        }
        self.addSubview(loadingPointLabel)
        self.loadingPointLabel.snp.makeConstraints{
            $0.left.equalTo(loadingLabel.snp.right)
            $0.bottom.equalTo(loadingLabel)
        }
    }
    
    func startLoading(){
        let animation = LottieAnimation.named("launch_loading")
        self.launchLoadingImageView.animation = animation
        self.launchLoadingImageView.play(fromProgress: 0, toProgress: 1,loopMode: .loop)
        
        self.timer = DispatchSource.makeTimerSource()
        self.timer?.schedule(deadline: .now(), repeating: 0.5)
        
        var count = 0
        var time: Float = 0.0
        self.timer?.setEventHandler(handler: {
            DispatchQueue.main.async{
                switch count {
                case 0:
                    self.loadingPointLabel.text = "."
                case 1:
                    self.loadingPointLabel.text = ".."
                case 2:
                    self.loadingPointLabel.text = "..."
                default:
                    break
                }
            }
            if !RdbSubscribeProductRequest.shared.products.isEmpty && time >= 3.0{
                self.finishTimer()
            }else{
                if time >= 6.0{
                    self.finishTimer()
                }
            }
            
            if count > 2{
                count = 0
            }else{
                count += 1
            }
            time += 0.5
        })
        self.timer?.resume()
    }
    
    func finishTimer(){
        self.timer?.cancel()
        self.timer = nil
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.25){
            self.completion?()
        }
    }
    
    private lazy var launchLoadingImageView: LottieAnimationView = {
        let launchLoadingImageView = LottieAnimationView()
        return launchLoadingImageView
    }()
    private lazy var loadingLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.white
        label.textAlignment = .center
        label.font = UIFont.regular(12)
        label.text = "Loading"
        return label
    }()
    private lazy var loadingPointLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.white
        label.textAlignment = .center
        label.font = UIFont.regular(12)
        return label
    }()
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
